/*
 * @Author: yangzonglong
 * @Date: 2021-02-25 14:35:42
 * @version: v1.0.0
 * @Descripttion: 
 * @LastEditors: TLL
 * @LastEditTime: 2021-05-06 09:08:02
 * @Auditor: 
 */
import React, { useCallback } from 'react';
import { DatePicker, TimePicker } from 'antd';
import moment, { Moment } from 'moment';
import { Icon } from '@library';

interface IProps {
  value: Moment;
  onChange(value: Moment | null): void;
  disabled?: boolean;
  required?: boolean;
  todayAndAfter?: boolean; // 为true时只能选择今天及以后的日期
  picker?: any;
  showTime?: boolean
}
interface PickerWithTypeProps extends IProps {
  disabledDate: ((date: Moment) => boolean) | undefined
}

function PickerWithType({ onChange, value, disabled, picker, disabledDate, showTime, required }: PickerWithTypeProps) {
  if (picker === 'time') return <TimePicker allowClear={!required} disabled={disabled} value={value} onChange={onChange} />;
  if (picker === 'date') return <DatePicker allowClear={!required} disabledDate={disabledDate} disabled={disabled} value={value} onChange={onChange} suffixIcon={<Icon icon="&#xe62d;" />} format={window.$app.MOMENT_FORMAT_DATE} />;
  return <DatePicker allowClear={!required} showTime={showTime} value={value} disabled={disabled} picker={picker} onChange={onChange} suffixIcon={<Icon icon="&#xe62d;" />} format={window.$app.MOMENT_FORMAT_DATE} />;
}

export default ({ value, onChange, disabled, todayAndAfter, picker, showTime, required }: IProps) => {

  const disabledDate = useCallback((currentDate: Moment) => {
    if (todayAndAfter) return moment(currentDate).isBefore(new Date(), "day");
    return false
  }, [todayAndAfter]);

  return <PickerWithType disabledDate={disabledDate} value={value} onChange={onChange}
    disabled={disabled} picker={picker} showTime={showTime} required={required}/>
}